package SocketServer.handler;

import SocketServer.common.ReqEntity;
import SocketServer.common.RespEntity;
import SocketServer.entity.Dict;
import SocketServer.entity.OptLog;
import SocketServer.service.DictServiceApi;
import com.alibaba.fastjson.JSON;

import java.util.List;

/**
 * 字典服务器处理器
 */
public class DictHandler {

//    private static Logger logger = LoggerFactory.getLogger(DicHandler.class);

    private static DictHandler dictHandler;

    /**
     * 私有构造方法
     */
    private DictHandler(){ }

    /**
     * 线程安全 单例模式创建方法
     * @return
     */
    public static synchronized DictHandler getInstance(){
        if(dictHandler == null){
            dictHandler = new DictHandler();
        }
        return dictHandler;
    }

    /**
     * 查询所有单词
     * @param reqEntity
     * @return
     */
    public String queryAllDict(ReqEntity reqEntity){
        try{
            DictServiceApi service = DictServicePool.borrowObject();//申请对象
            List<Dict> DictLists = service.queryAllDict( reqEntity );
            DictServicePool.returnObject(service);//归还对象
            return JSON.toJSONString(new RespEntity(0,"查询成功！",DictLists));
        }catch (Exception exception){
            exception.printStackTrace();
            return JSON.toJSONString(new RespEntity(-1,exception.getMessage(),null));
        }
    }
    /**
     * 查找单词意思
     * @param reqEntity
     * @return
     */
    public String findDict(ReqEntity reqEntity){
        try{
            DictServiceApi service = DictServicePool.borrowObject();//申请对象
            Dict dict = service.findDict(reqEntity);
            service.saveOptLog( reqEntity );//存储操作日志
            DictServicePool.returnObject(service);//归还对象
            if(dict == null){
                return JSON.toJSONString(new RespEntity(0,"无该单词！",null));
            }else{
                return JSON.toJSONString(new RespEntity(0,"查询成功！",dict.getMeaning()));
            }
        }catch (Exception exception){
            exception.printStackTrace();
            return JSON.toJSONString(new RespEntity(-1,exception.getMessage(),null));
        }
    }

    /**
     * 新增单词
     * @param reqEntity
     * @return
     */
    public String insertDict(ReqEntity reqEntity){
        try{
            //先查询单词是否存在
            DictServiceApi service = DictServicePool.borrowObject();//申请对象
            Dict dict = service.findDict(reqEntity);
            service.saveOptLog( reqEntity );//存储操作日志
            if(dict!=null){
                return JSON.toJSONString(new RespEntity(9,"该单词已存在！",null));
            }
            String[] dataList = reqEntity.getJsonData().split( " " );
            boolean flag = service.insertDict(reqEntity);
            DictServicePool.returnObject(service);//归还对象
            if(flag == true){
                return JSON.toJSONString(new RespEntity(0,"新增成功！",null));
            }else{
                return JSON.toJSONString(new RespEntity(9,"新增失败！",null));
            }
        }catch (Exception exception){
            exception.printStackTrace();
            return JSON.toJSONString(new RespEntity(-1,exception.getMessage(),null));
        }
    }

    /**
     * 删除单词
     * @param reqEntity
     * @return
     */
    public String deleteDict(ReqEntity reqEntity) {
        try{
            //先查询单词是否存在
            DictServiceApi service = DictServicePool.borrowObject();//申请对象
            Dict dict = service.findDict(reqEntity);
            if(dict==null){
                return JSON.toJSONString(new RespEntity(9,"该单词不存在！",null));
            }
            boolean flag = service.deleteDict(reqEntity);
            DictServicePool.returnObject(service);//归还对象
            if(flag == true){
                return JSON.toJSONString(new RespEntity(0,"删除成功！",null));
            }else{
                return JSON.toJSONString(new RespEntity(9,"删除失败！",null));
            }
        }catch (Exception exception){
            exception.printStackTrace();
            return JSON.toJSONString(new RespEntity(-1,exception.getMessage(),null));
        }
    }

    /**
     * 修改单词
     * @param reqEntity
     * @return
     */
    public String updateDict(ReqEntity reqEntity){
        try{
            //先查询单词是否存在
            DictServiceApi service = DictServicePool.borrowObject();//申请对象
            Dict dict = service.findDict(reqEntity);
            if(dict==null){
                return JSON.toJSONString(new RespEntity(9,"该单词不存在！",null));
            }
//            String[] dataList = reqEntity.getJsonData().split( " " );
            boolean flag = service.updateDict(reqEntity);
            DictServicePool.returnObject(service);//归还对象
            if(flag == true){
                return JSON.toJSONString(new RespEntity(0,"更新成功！",null));
            }else{
                return JSON.toJSONString(new RespEntity(9,"更新失败！",null));
            }
        }catch (Exception exception){
            exception.printStackTrace();
            return JSON.toJSONString(new RespEntity(-1,exception.getMessage(),null));
        }
    }

    /**
     * 查询所有的操作列表
     * @param reqEntity
     * @return
     */
    public String findOptLogList(ReqEntity reqEntity){
        try{
            DictServiceApi service = DictServicePool.borrowObject();//申请对象
            List<OptLog> optLogLists = service.findOptLogList( reqEntity );
            DictServicePool.returnObject(service);//归还对象
            return JSON.toJSONString(new RespEntity(0,"查询成功！",optLogLists));
        }catch (Exception exception){
            exception.printStackTrace();
            return JSON.toJSONString(new RespEntity(-1,exception.getMessage(),null));
        }
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {

    }


}
